package com.haiyi.activiti.engine.framework.database;

import com.haiyi.activiti.engine.exception.TooManyResultException;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p> mybatisPlus本地查询扩展类 </p>
 *
 * <pre> Created: 2018/12/5 14:24 </pre>
 *
 * @author hlu
 * @version 1.0
 * @since JDK 1.7
 */
public abstract class AbstractNativeServiceImpl <M extends HaiyiMapper<T,Q> & NativeQuery<T>,
        T extends Serializable,Q extends Query<? extends Query<?,?>,T>>
        extends AbstractServiceImpl<M,T,Q> implements NativeQueryService<T> {

    @Override
    public T findOneUserByNativeQuery(Map<String, Object> parameterMap) {
        final List<T> users = findUsersByNativeQuery(parameterMap);
        if(CollectionUtils.isEmpty(users)){
            return null;
        }else if(users.size() == 1){
            return users.get(0);
        }else{
            throw new TooManyResultException("native查询异常，期望查询一条结果，但实际结果条数为："+users.size());
        }
    }

    @Override
    public List<T> findUsersByNativeQuery(Map<String, Object> parameterMap) {
        return baseMapper.findUsersByNativeQuery(parameterMap);
    }

    @Override
    public long findUserCountByNativeQuery(Map<String, Object> parameterMap) {
        return baseMapper.findUserCountByNativeQuery(parameterMap);
    }
}
